import functools

from flask import current_app, jsonify
from flask import g
from flask import session

from info.utils.response_code import RET


def do_index_class(index):
    """根据index下标返回对应的class值"""

    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


def user_login_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        user = None  # type:User

        # 延迟导入，解决循环导入db的问题
        from info.models import User

        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
                return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")
        g.user = user
        return view_func(*args, **kwargs)
    return wrapper